#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/23 17:33
# @Author  : Beam
# @Site    : 
# @File    : authentication.py
# @Software: PyCharm

import hashlib,time
import requests
import json

class Client(object):
    def __init__(self):
        self.key = 'ucuxeyaBusiep6chei5aicohnaeNgei2'    #server端定义提供的key
        self.key_name = 'auth-key'  #该值为固定的参数
        self.asset_api = 'http://127.0.0.1:8000/api/asset/'  #接口url

    def auth_key(self):
        """
        接口认证
        """
        ha = hashlib.md5(self.key.encode('utf-8'))      # 用self.key 做加密盐
        time_span = time.time()
        ha.update(bytes("%s|%f" % (self.key, time_span), encoding='utf-8'))
        encryption = ha.hexdigest()
        result = "%s|%f" % (encryption, time_span)
        return {self.key_name: result}

    def get_asset(self,data):
        """
        get方式向接口提交json数据
        @post方式在脚本里会有csrf验证
        """
        headers = {}
        headers.update(self.auth_key())  #把auth_key()这个方法里返回的字典加入到headers
        # print(headers)
        response = requests.get(
            url=self.asset_api,
            headers=headers,
	        data=data,
        )
        print(response.text)


s = {'a':1,'b':2}
Client().get_asset(json.dumps(s))